<?php
/*
 * File: OrderDetails.php
 * Project: NewDemo
 * Created Time: 2024-07-09 10:32:55 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-15 3:28:05 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */


namespace App\Models;

use DateTimeInterface;
use App\Models\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;


/**
 * App\Models\OrderSnapshot
 *
 */

class OrderLogistics extends Base{

    protected $table = 'order_logistics';
    use Notifiable;
    public $timestamps = true;
    /**
     * 为数组 / JSON 序列化准备日期。
     */
    protected function serializeDate(DateTimeInterface $date){
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }



    /**
     * 函數構造
     */
    public function __construct(){
        $this->language = app('global_language') == "en" ? "en_" : "";
    }
/*********************************************************全局作用域注册**********************************************************************/
    protected static function boot(){
       parent::boot();
	   static::addGlobalScope('order_logistics', function (Builder $builder) {
		  return $builder->where('order_logistics.logistics_status', '<>', -1);
	   });
    }

/*********************************************************admin**********************************************************************/

    /**
     * 添加訂單物流
     * @param $data
     * @return array
     */
    public function addOrderLogisticsWeb($data){
        DB::beginTransaction();
        $addNumber = $this->where('logistics_id',$data['logistics_id'])->update(['logistics_number'=>$data['logistics_number'],'logistics_status'=>1]);
        $updateDetails = OrderDetails::where('details_id',$data['details_id'])->update(['logistics_status'=>1]);
        if($addNumber && $updateDetails){
            DB::commit();
            return $this->success(__('web.success'));
        }else{
            DB::rollBack();
            return $this->error(__('web.network_error'));
        }
    }

/**********************************************************api**********************************************************************/


/**************************************************common(api和admin共用)***********************************************************/


    /**
     * @param $token
     * @return mixed
     * 添加訂單詳情物流
     */
    public function addOrderLogistics($user_id , $details_id){
        DB::beginTransaction();
        try {
            $data['user_id'] = $user_id;
            $data['details_id'] = $details_id;
            $data['logistics_status'] = 1;
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $effects= $this->insert($data);
            if(!empty($effects)){
                // $this->LogInfo('添加收貨地址' , $effects , 'addUserAddress' , true);//开启日志记录
                DB::commit();   return $effects;
            }else{
                DB::rollBack();   return false;
            }
        }catch (\Exception $e) {
            DB::rollBack();   return  $e->getMessage();
        }
    }




/**************************************************end***********************************************************/







}
